package com.smh.a11Test5;

import java.io.*;
import java.net.Socket;
import java.util.UUID;

public class MyThread extends Thread {
    Socket socket;

    public MyThread(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        synchronized (MyThread.class) {
            try {
                InputStream is = socket.getInputStream();
                BufferedInputStream bis = new BufferedInputStream(is);

                //写到本地
                String uuid = UUID.randomUUID().toString().replace("-", "");
                FileOutputStream fos = new FileOutputStream("day30-code\\file\\" + uuid + ".txt");
                BufferedOutputStream bos = new BufferedOutputStream(fos);
                int b;
                while ((b = bis.read()) != -1) {
                    bos.write((char) b);
                    System.out.println((char) b);
                }
                bos.flush();
                bos.close();
//        socket.shutdownInput();//这只是给一个标志,不会关流
//        bis.close();


                //向客户端返回数据
                BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                String s = "年后在说";
                bw.write(s);
                bw.newLine();
                bw.flush();
                bw.close();

                System.out.println("zhixingdaol1");
            } catch (IOException e) {
                throw new RuntimeException(e);
            } finally {
                try {
                    socket.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

        }
    }
}
